/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.web;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.utils.DateUtils;
import cn.mcsys.myadmin.common.utils.GpsUtils;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.modules.air.dao.AirCarLogDao;
import cn.mcsys.myadmin.modules.air.entity.*;
import cn.mcsys.myadmin.modules.air.service.AirCarRecordService;
import cn.mcsys.myadmin.modules.air.service.AirCarService;
import cn.mcsys.myadmin.modules.air.service.AirDriverService;
import cn.mcsys.myadmin.modules.air.service.AirRidePointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆管理Controller
 * @author leopold
 * @version 2018-08-13
 */
@Controller
@RequestMapping(value = "${adminPath}/air/airCar")
public class AirCarController extends BaseController {

	@Autowired
	private AirCarService airCarService;

	@Autowired
	private AirDriverService driverService;

	@Autowired
	private AirRidePointService airRidePointService;

	@Autowired
	private ServletContext servletContext;

	@Autowired
	AirCarRecordService airCarRecordService;

	@Autowired
	AirCarLogDao airCarLogDao;

	@RequiresPermissions("air:airCar:view")
	@RequestMapping(value = {"list", ""})
	public String list(AirCar airCar, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AirCar> page = airCarService.findPage(new Page<AirCar>(request, response), airCar);
		List<AirRidePoint> ridePoints = airRidePointService.findAllList();
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format1 = format.format(date);
		System.out.println("当前时间："+format1);
		model.addAttribute("messageText","请输入车牌查询");
		model.addAttribute("rides",ridePoints);
		model.addAttribute("page", page);

		String realPath1 = request.getSession().getServletContext().getRealPath("/");
		return "modules/air/airCarList";
	}

	@RequiresPermissions("air:airCar:view")
	@RequestMapping(value = "form")
	public String form(AirCar airCar, Model model) {
	    if (airCar.getId() != null){
            airCar = airCarService.getAll(airCar.getId());
        }
		List<AirDriver> drivers = driverService.findAllList(new AirDriver());
		List<AirRidePoint> rides = airRidePointService.findAllList(new AirRidePoint());
		model.addAttribute("airCar", airCar);
		model.addAttribute("drivers",drivers);
		model.addAttribute("rides",rides);
		return "modules/air/airCarForm";
	}

	@RequiresPermissions("air:airCar:edit")
	@RequestMapping(value = "save")
	public String save(AirCar airCar, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, airCar)){
			return form(airCar, model);
		}
		airCarService.save(airCar);
//		addMessage(redirectAttributes, "保存车辆成功");
		return "redirect:"+Global.getAdminPath()+"/air/airCar/?repage";
	}
	
	@RequiresPermissions("air:airCar:edit")
	@RequestMapping(value = "delete")
	public String delete(AirCar airCar, RedirectAttributes redirectAttributes) {
		airCarService.delete(airCar);
//		addMessage(redirectAttributes, "删除车辆成功");
		return "redirect:"+Global.getAdminPath()+"/air/airCar/?repage";
	}

	@RequestMapping(value = "getCarMessage",method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getCarMessage(Long carid){
		AirCar airCar = airCarService.get(carid.intValue());
		if(airCar.getLon()!=null&&airCar.getLat()!=null) {
			GpsUtils gpsUtils = GpsUtils.gps84_To_Gcj02(Double.valueOf(airCar.getLat()).doubleValue(), Double.valueOf(airCar.getLon()).doubleValue());
			airCar.setLon(gpsUtils.getMgLon() + "");
			airCar.setLat(gpsUtils.getMgLat() + "");
		}
		return  JsonResult.success(airCar);
	}

	@RequestMapping(value = "getCarRecord",method = RequestMethod.GET)
	@ResponseBody
	public JsonResult getCarRecord(Long carid , Date beginDate,Date endDate, HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> result = new HashMap<>();
		// 设置默认时间范围，默认当前月
		if (beginDate == null){
			beginDate = DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1);
		}
		if (endDate == null){
			endDate = DateUtils.addMonths(beginDate, 1);
		}
		List<AirCarRecord> list = airCarRecordService.getCarRecord(carid,beginDate,endDate);
		result.put("recordList",list);
		AirCar airCar = airCarService.get(carid.intValue());
		result.put("airCar",airCar);
		// todo 轨迹
		List<AirCarLog> carLogList = airCarLogDao.getCarLogByCarNumber(airCar.getCarNumber(), beginDate, endDate);
		for (AirCarLog log : carLogList) {
			GpsUtils gpsUtils = GpsUtils.gps84_To_Gcj02(Double.valueOf(log.getLat()).doubleValue(), Double.valueOf(log.getLon()).doubleValue());
			log.setLon(gpsUtils.getMgLon() + "");
			log.setLat(gpsUtils.getMgLat() + "");
		}
		result.put("trackList",carLogList);
		return  JsonResult.success(result);
	}
}